package com.powernode.controller;

import com.powernode.framework.Result;
import com.powernode.framework.ResultBean;
import com.powernode.framework.ResultUtil;
import com.powernode.pojo.FloorAdmin;
import com.powernode.pojo.TowerAdmin;
import com.powernode.service.FloorAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ycc
 * @Date: 2023/2/14 12:25
 */
@RestController
@RequestMapping("/api/floorAdmin")
public class FloorAdminController {
    @Autowired
    FloorAdminService floorAdminService;

    @PostMapping("/save")
    public Result<String> modify(@RequestBody FloorAdmin floorAdmin) {
        if(floorAdmin.getId()!=null)
            floorAdminService.modify(floorAdmin);
        else
            floorAdminService.add(floorAdmin);
        return ResultUtil.ok();
    }
    @PostMapping("/remove")
    public Result<String> remove(@RequestBody Integer[] ids) {
        floorAdminService.removes(ids);
        return ResultUtil.ok();
    }
    @GetMapping("/list")
    public ResultBean<List<FloorAdmin>> getPageResult(
            HttpServletRequest request,
            @RequestParam(required = false) String floorSite,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String studentNumber,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit) {

        FloorAdmin floorAdmin = (FloorAdmin) request.getSession().getAttribute("floorAdmin");
        if ( floorSite == null || floorSite.equals("") ){
            floorSite = floorAdmin.getFloorSite();
        }else {
            if(!floorSite.substring(0,5).equals(floorAdmin.getFloorSite())){
                return new ResultBean<>(0, "自己不可修改负责楼层！", 0, null);
            }
        }

        Map<String, Object> param = new HashMap<>(16);
        int offset = (page - 1) * limit;
        int rows = limit;
        param.put("floorSite",floorSite);
        param.put("name",name);
        param.put("studentNumber",studentNumber);
        param.put("offset", offset);
        param.put("rows", rows);
        int totalRows = floorAdminService.count(param);
        List<FloorAdmin> entities = floorAdminService.getPageResult(param);
        return new ResultBean<>(0, "查询成功", totalRows, entities);
    }
}
